package com.fit.hibernate;

import java.io.Serializable;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.beans.UserBean;
import com.fit.interfaces.UserI;
import com.fit.service.UsersI;

public class UsersServiceHibernate implements UsersI {

	private Session session;
	
	
	
	
	public UsersServiceHibernate(Session session) {
		this.session = session;
	}

	public UserI save(UserI user) {
		return (UserI) session.get(UserBean.class,session.save(user));
	}

	public Collection<UserI> retrive() {
		Criteria criteria = session.createCriteria(UserBean.class);
		return criteria.list();
	}

	public UserI retriveSingleUser(Serializable userId) {
		
		return (UserI) session.get(UserBean.class, userId);
	}

	public UserI retriveSingleUser(String userName) {
		Criteria criteria = session.createCriteria(UserBean.class).add(Restrictions.eq("userName", userName));
		
		
		
		return (UserI) criteria.uniqueResult();
	}

}
